#pragma once
#include <string>
#include <iostream>
#include <list>

using namespace std;


class GameText
{
private:
	LPD3DXFONT           Font;
	D3DCOLOR             FontColor;   
	RECT                 StatusRect;
	RECT                 InfoRect;
	std::wstring Text;
	int fps;
	D3DXVECTOR2 position;
	list<string> InfoList;
	DWORD LastChangedTime;

public:

	GameText(LPDIRECT3DDEVICE9 device)
	{
		FontColor = D3DCOLOR_ARGB(255,40,40,40);

		StatusRect.left = 2;
		StatusRect.top = 2;
		StatusRect.right = (LONG)ScreenWidth;
		StatusRect.bottom = 40;

		InfoRect.left = 2;
		InfoRect.top = 40;
		InfoRect.right = (LONG)ScreenWidth;
		InfoRect.bottom = 75;

		// Create a D3DX font object
		HRESULT hr = D3DXCreateFont(device, 21, 9, FW_NORMAL, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
				ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &Font );

		if(FAILED(hr))
			MessageBox(NULL,L"Call to D3DXCreateFont failed!", L"ERROR",MB_OK|MB_ICONEXCLAMATION);

		position = D3DXVECTOR2(0,0);
		fps = 0;
		LastChangedTime = timeGetTime();

	}

	~GameText()
	{
		
	}

	void Release()
	{
		if (Font != NULL)
		{
			Font->Release();
			Font = NULL;
		}
		
		FontColor = NULL;
	}

	void SetFps(int TempFps)
	{
		fps = TempFps;
	}

	void SetPosition(D3DXVECTOR2 positionVec)
	{
		position = positionVec;
	}


	void AddInfo(string infoText)
	{
		InfoList.push_back(infoText);
		if (InfoList.size() > 10)
		{
			InfoList.pop_front();
		}
		LastChangedTime = timeGetTime();
	}


	void Draw(LPD3DXSPRITE SpriteBatch)
	{
		if (LastChangedTime  <(timeGetTime() - 3000))
		{
			if (InfoList.size() > 0)
			{
				InfoList.pop_front();
				LastChangedTime += 600;
			}
		}

		D3DXMATRIX Matrix;
		D3DXMatrixIdentity(&Matrix);
		SpriteBatch->SetTransform(&Matrix);

		std::string AppStatus = 
			"FPS: " + ToString(fps) +
			"\nPosition  X:" + ToString(position.x) + " Y:" + ToString(position.y);

		Font->DrawTextA(SpriteBatch, AppStatus.c_str(), -1, &StatusRect, 0, FontColor);

		
		list<string>::iterator i;
		for (i = InfoList.begin(); i != InfoList.end(); ++i)
		{
			Font->DrawTextA(SpriteBatch, (*i).c_str(), -1, &InfoRect, 0, FontColor);
			InfoRect.top += 20;
			InfoRect.bottom += 20;
		}

		InfoRect.top = 40;
		InfoRect.bottom = 75;
	}
};